package com.orangelife.mobile.voucher.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.curry.android.http.VolleyHttpUtil;
import com.curry.android.util.IntentHelper;
import com.curry.android.util.ToastHelper;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.common.jump.JumpManager;
import com.orangelife.mobile.voucher.adapter.VoucherListSelectAdapter;
import com.orangelife.mobile.confirmOrder.biz.MerchantOrderListHelper;
import com.orangelife.mobile.voucher.biz.VoucherBiz;
import com.orangelife.mobile.voucher.biz.VoucherHttpBiz;
import com.orangelife.mobile.voucher.biz.SelectedVoucherIDListHelper;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 抵用券列表，可选择模式（多选或单选）.多商户（商户ID），单商品（按类型分，不需要ID，比如停车卡、洗车、缴费）
 * （这个类的核心是VoucherBiz，将被改成ListHelper，用于保存选中的VoucherID，这就对使用方法产生一个问题）
 * （谁知道，存在一个VoucherBiz？他保存了选中的抵用券ID列表？谁知道？当初之所以选择这个，因为另外一个界面还在对选中的进行处理）；
 * （因为，还会再度进入，所以单例还是有必要的，必须文档，否则很难直观的理解这个单例BIZ；）
 * Create by cfr .Modify by wgf on 160226
 *
 * @author cfr, wgf
 */
public class VoucherListSelectActivity extends OrangeLifeBaseActivity {

    /** 标题中间 */
    private TextView tvTitle;
    /** 标题右边 */
    private TextView tvTitleRight;
    private LinearLayout ib_title_left;
    /** 列表 */
    private ListView pullListView;
    /** 没有抵用券 */
    private LinearLayout llNoVoucher;
    /** 线上支付（现在暂时没用） */
    private Button btnOnlinePay;
    /** 没有抵用券的图片 */
    private ImageView ivNoBill;
    /** 没有抵用券的描述 */
    private TextView tvNoBillDes;
    /** 没有抵用券描述2 */
    private TextView tvNoBillDes2;
    private VoucherListSelectAdapter<Map<String, Object>> adapter;
    private List<Map<String, Object>> mVoucherList = new ArrayList<>();
    /** 商品、洗车、停车卡、缴费 */
    private int mType;
    /** 多商户商品订单所需参数：merID & totalCost */
    private String mMerId;
    private String mGoodID;
    private String mTotalCost;
    /*2016/3/8 两个Helper和这种极为特殊的Activity传值方式，还有这种抵用券列表的key，都必须详细说明；还有最高金额，也是要说明*/


    /** 单个商品或服务，只需要goodID */

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_voucher_list);
        getIntentInfo();
        initView();
        setAdapter();
        requestCurrentTypeVoucherList();
    }

    private void getIntentInfo() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            mGoodID = bundle.getString(MerchantOrderListHelper.GOOD_ID);
            mType = bundle.getInt(MerchantOrderListHelper.TYPE);
            mMerId = bundle.getString(MerchantOrderListHelper.MER_ID);
            mTotalCost = bundle.getString(MerchantOrderListHelper.TOTAL_COST);
        }
    }

    private void initView() {
        tvTitle = (TextView) findViewById(R.id.tv_title);
        ib_title_left = (LinearLayout) findViewById(R.id.ib_title_left);
        tvTitleRight = (TextView) findViewById(R.id.tv_title_right);
        pullListView = (ListView) findViewById(R.id.lvVoucher);
        llNoVoucher = (LinearLayout) findViewById(R.id.llNoBill);
        btnOnlinePay = (Button) findViewById(R.id.btnOnlinePay);
        ivNoBill = (ImageView) findViewById(R.id.ivNoBillGoods);
        tvNoBillDes = (TextView) findViewById(R.id.tvNoBillDes);
        tvNoBillDes2 = (TextView) findViewById(R.id.tvNoBillDes2);
        tvTitle.setText(R.string.my_voucher);
        tvTitleRight.setText(R.string.rule);
        btnOnlinePay.setOnClickListener(listener);
        tvTitleRight.setOnClickListener(listener);
    }

    private OnClickListener listener = new OnClickListener() {

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.btnOnlinePay:/*已废弃*/
                    break;
                case R.id.tv_title_right:
                    jumpToVoucherRule();
                    break;
                default:
                    break;
            }
        }
    };

    public void onBackClick(View v) {
        jumpBack();
    }


    /** 设置适配器 */
    @SuppressWarnings("unchecked")
    private void setAdapter() {
        adapter = new VoucherListSelectAdapter<>(this, mVoucherList);
        pullListView.setAdapter(adapter);
    }

    /** 请求当前商家的抵用券列表 */
    private void requestCurrentTypeVoucherList() {
        showDialog();
        if (mType == VoucherHttpBiz.TYPE_GOOD) {
            VoucherHttpBiz.getVoucherList(mType, mMerId, mTotalCost, mGoodID, callback);
        } else if (mType == VoucherHttpBiz.TYPE_REPAIR) {
            VoucherHttpBiz.getVoucherList(mType, null, mTotalCost, null, callback);
        } else if (mType == VoucherHttpBiz.TYPE_WASH_CAR || mType == VoucherHttpBiz.TYPE_PROPERTY) {
            VoucherHttpBiz.getVoucherList(mType, mMerId, mTotalCost, mGoodID, callback);
        } else {
            closeDialog();
            ToastHelper.getInstance()._toast("对不起，暂无此类型抵用券，请联系客服");
        }
    }

    VolleyHttpUtil.ResponseCallBack callback = new VolleyHttpUtil.ResponseCallBack() {
        @Override
        public void handleResponse(JSONObject response, int errCode) {
            closeDialog();
            List<Map<String, Object>> voucherList = VoucherHttpBiz.handleVoucherList(response, adapter);
            if (voucherList != null) {
                mVoucherList = voucherList;
                adapter.notifyDataSetChanged();
            }
        }
    };

    private void jumpToVoucherRule() {
        VoucherBiz.jumpToVoucherRule(this);
    }

    /** 跳回上一个界面，抵用券列表是个公共的类 */
    private void jumpBack() {
        adapter.finishSelectedVoucher();
        Intent intent = new Intent();
        String totalPrice = adapter.getTotalSelectedVoucherPrice();
//        String totalVoucherNum = String.valueOf(adapter.getTotalVoucherNum());
//        intent.putExtra(.TOTAL_COST, totalPrice);
        SelectedVoucherIDListHelper.getInstance().setTotalSelectedVoucherPrice(totalPrice);
//        intent.putExtra(.MER_ID, mMerId);
        /*since v3.4.2,已不需要此参数*/
//        intent.putExtra("totalVoucherNum", totalVoucherNum);
        setResult(JumpManager.RESULT_CODE_VOUCHER_LIST, intent);
        finish();
        IntentHelper.getInstances().jumpActivityAnimation(this, true);
    }

    @Override
    public void onBackPressed() {
        jumpBack();
    }
}

